package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductFmlBasic;
import com.qckj.amz.service.IProductFmlAttrService;
import com.qckj.amz.service.IProductFmlBasicService;
import com.qckj.amz.service.IProductFmlDescService;
import com.qckj.amz.service.IProductFmlRlatService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 产品基础信息Controller
 *
 * @author luchuan
 * @since 1.0 2021-12-20
 */
@RestController
@RequestMapping("/amz/productFmlBasic")
public class ProductFmlBasicController extends BaseController {
    @Autowired
    private IProductFmlBasicService productFmlBasicService;
    @Autowired
    private IProductFmlRlatService productFmlRlatService;
    @Autowired
    private IProductFmlDescService productFmlDescService;
    @Autowired
    private IProductFmlAttrService productFmlAttrService;

    /**
     * 查询产品基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductFmlBasic productFmlBasic) {
        Page<ProductFmlBasic> page = productFmlBasicService.findProductFmlPage(productFmlBasic);
        return getDataTable(page);
    }

    /**
     * 获取产品基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(productFmlBasicService.findById(id));
    }

    /**
     * 修改产品基础信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:edit')")
    @Log(title = "产品基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductFmlBasic productFmlBasic) {
        productFmlBasicService.save(productFmlBasic);
        return AjaxResult.success(productFmlBasic);
    }

    /**
     * 删除产品基础信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:remove')")
    @Log(title = "产品基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productCodes}")
    public AjaxResult remove(@PathVariable String[] productCodes) {
        List<String> productCodeList = Arrays.asList(productCodes);
        //通过productCode删除产品相关数据
        //暂时不处理，后期改用逻辑删除
        productFmlBasicService.deleteByProductCodes(productCodeList);
        productFmlRlatService.deleteByProductCodes(productCodeList);
        productFmlDescService.deleteByProductCodes(productCodeList);
        productFmlAttrService.deleteByProductCodes(productCodeList);
        return AjaxResult.success();
    }

    /**
     * 通过产品代码获取产品信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:query')")
    @RequestMapping(value = "/getListByProductCode")
    public AjaxResult getListByProductCode(@RequestBody String productCode) {
        AjaxResult res = productFmlBasicService.getListByProductCode(productCode);
        return res;
    }

    /**
     * 新增产品基本信息和附加属性
     * @param formData
     *             var formData ={
     *               productFmlBasic:this.form,
     *               productFmlAttr:this.formAttr
     *             }
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:add')")
    @PostMapping("/addProductFmlBasic")
    public AjaxResult addProductFmlBasic(@RequestBody Map<String, Object> formData) {
        try {
            AjaxResult res = productFmlBasicService.addProductFmlBasic(formData);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改产品基本信息和附加属性
     * @param formData
     *             var formData ={
     *               productFmlBasic:this.form,
     *               productFmlAttr:this.formAttr
     *             }
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:edit')")
    @PostMapping("/updateProductFmlBasic")
    public AjaxResult updateProductFmlBasic(@RequestBody Map<String, Object> formData) {
        try {
            AjaxResult res = productFmlBasicService.updateProductFmlBasic(formData);
            return res;
        }catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

    }

    /**
     * 导出产品
     * @param productCategory   产品分类
     * @param productCodes      产品代码
     * @param countryCodes      国家代码
     * @param productInitId    模板id(常用属性)
     * @param externalProductIdType    商品编码类型
     * @param generateExternalProductIdType    商品编码生成方式：1重新生成；0补齐生成（默认）
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:export')")
    @Log(title = "导出产品", businessType = BusinessType.EXPORT)
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    public AjaxResult export(@RequestParam(name = "productCategory") String productCategory,
                             @RequestParam(name = "productCodes") List<String> productCodes,
                             @RequestParam(name = "countryCodes") List<String> countryCodes,
                             @RequestParam(name = "productInitId") String productInitId,
                             @RequestParam(name = "externalProductIdType") String externalProductIdType,
                             @RequestParam(name = "generateExternalProductIdType") String generateExternalProductIdType
    ) {

        AjaxResult res = productFmlBasicService.exportProductFml(productCategory,productCodes,countryCodes,productInitId,externalProductIdType,generateExternalProductIdType);
        return res;
    }

    /**
     * 产品翻译
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:translate')")
    @Log(title = "产品翻译", businessType = BusinessType.TRANSLATE)
    @GetMapping("/translate/{productCodes}")
    public AjaxResult translate(@PathVariable List<String> productCodes) {
        if (productCodes==null || productCodes.isEmpty()){
            return AjaxResult.error("产品代码不能为空！");
        }
        AjaxResult res = productFmlBasicService.translate(productCodes);
        return res;
    }

    /**
     * 上传产品
     * @param productCodes      产品代码
     * @param shopCode          店铺编号
     * @param countryCodes      国家代码
     * @param productInitId    模板id(常用属性)
     * @return
     */
    @PreAuthorize("@ss.hasPermi('amz:productFmlBasic:uploadToAmazon')")
    @Log(title = "上传产品", businessType = BusinessType.UPLOADTOAMAZONSUBMIT)
    @RequestMapping(value = "/uploadToAmazonSubmit", method = RequestMethod.POST)
    public AjaxResult uploadToAmazonSubmit(@RequestParam(name = "productCodes") List<String> productCodes,
                             @RequestParam(name = "shopCode") String shopCode,
                             @RequestParam(name = "countryCodes") List<String> countryCodes,
                             @RequestParam(name = "productInitId") String productInitId
    ) {
        AjaxResult res = productFmlBasicService.uploadToAmazonSubmit(productCodes,shopCode,countryCodes,productInitId);
        return res;
    }

}
